const ws = require("ws");
const server = new ws.Server({ port: process.env.PORT || 3000 });
const userList = {
    10: { userid: 10, uname: "客服", avatar: "https://dummyimage.com/50x50/4A7BF7/FFF&text=%E5%AE%A2" }
}
const clientList = {};
const messageList = {}
server.on("connection", connectionHandler);


function connectionHandler(client) {
    client.on("message", (msg) => messageHandler(msg, client));
    client.on("close", () => closeHandler(client))
    client.on("error", () => closeHandler(client))
    setInterval(heartbeat,3000)
}
function heartbeat() {
    for (var key in clientList) {
        if (Date.now() - clientList[key].time > 6000) {
            closeHandler(clientList[key].client)
        }
    }
}
function messageHandler(msg, client) {
    const result = JSON.parse(msg);
    switch (result.type) {
        case 0x01:
            userList[result.data.userid] = result.data;
            client.userid = result.data.userid;
            clientList[result.data.userid] = { client, time: Date.now() }
            for (var key in clientList) {
                clientList[key].client.send(JSON.stringify({ type: 0x02, data: Object.values(userList).filter(item => item.userid != key) }))
            }
            break;
        case 0x03:
            list = getUserMessageList(result.data.userid, result.data.targetid);
            client.send(JSON.stringify({ type: 0x04, data: list }))
            break;
        case 0x05:
            if (!userList[result.data.userid] || !userList[result.data.targetid]) {
                client.send(JSON.stringify({ type: 0x06, data: { message: "发送的聊天对象已经下线！" } }))
                return;
            }
            list = getUserMessageList(result.data.userid, result.data.targetid);
            const user = userList[result.data.userid];
            const target = userList[result.data.targetid];
            list.push({
                userid: user.userid,
                userUname: user.uname,
                userAvatar: user.avatar,
                targetid: target.userid,
                targetUname: target.uname,
                targetAvatar: target.avatar,
                msgTime: result.data.msgTime,
                msgType: result.data.msgType,
                message: result.data.message
            })
            client.send(JSON.stringify({ type: 0x04, data: list }))
            if (clientList[result.data.targetid] && clientList[result.data.targetid].client) clientList[result.data.targetid].client.send(JSON.stringify({ type: 0x04, data: list }))
            break;
        case 0x07:
           if(clientList[result.data.userid])  clientList[result.data.userid].time = Date.now()
            break;
    }
}

function closeHandler(client) {
    delete clientList[client.userid]
    delete userList[client.userid];
    for (var key in userList) {
        if (userList[key].client) userList[key].client.send(JSON.stringify({ type: 0x02, data: Object.values(userList).filter(item => item.userid != key) }))
    }
    client.close()
}

function getUserMessageList(userid, targetid) {
    if (messageList[userid + "-" + targetid] || messageList[targetid + "-" + userid]) {
        return messageList[userid + "-" + targetid] || messageList[targetid + "-" + userid]
    }
    messageList[userid + "-" + targetid] = [];
    return messageList[userid + "-" + targetid];
}
 